跳到主要内容

Starship 美化终端

3 分钟阅读

前言

随着 Windows 11 的逐渐稳定,微软也将 Windows Terminal 收录到新版本的Windows 操作系统中,使其作为 Windows 的默认终端界面。

虽然 Windows Terminal 已经做得很好了,但是相比于主流 Linux 发行版和 MAC 的终端界面,Windows Terminal 可自定义的样式还是太少了还缺少对命令行输出的美化,因此我选择使用 Starship 来补齐这块短板,定制一套简洁、美观且高效的终端。

开始

下载并安装 Starship

官方网站

下载地址

配置环境

下载并安装 Starship 后将以下内容添加到 PowerShell 配置文件的末尾(通过运行 $PROFILE 来获取配置文件的路径)

默认返回的文件路径

C:\Users\用户名\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

将以下内容粘贴进 Microsoft.PowerShell_profile.ps1 文件内

Invoke-Expression (&starship init powershell)

这时再重新启动 PowerShell 可能会出现

powershell. : 无法加载文件 C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚本。

不用担心,我们通过管理员权限运行 PowerShell,并输入以下内容

 set-ExecutionPolicy RemoteSigned

如果想要改回默认则输入以下内容即可

set-executionpolicy restricted

再次开启 PowerShell 就可以看到美化后的界面了

配置文件路径

默认的配置文件路径在 C:\Users\用户名\.config\starship.toml

您也可以使用 STARSHIP_CONFIG 环境变量更改默认配置文件的位置

export STARSHIP_CONFIG=~/example/non/default/path/starship.toml

在 PowerShell (Windows) 中,在 $PROFILE 中添加下面的代码行能达到同样的效果

$ENV:STARSHIP_CONFIG = "$HOME\example\non\default\path\starship.toml"

配置分享

在这里附上我的 Starship 配置文件

# ~/.config/starship.toml

format = """
$time \
$username\
$directory \
$character\
"""

add_newline = false

[battery]
full_symbol = "🔋"
charging_symbol = "🔌"
discharging_symbol = "⚡"

[[battery.display]]
threshold = 30
style = "bold red"

[character]
error_symbol = "[❌](bold red)"
#success_symbol = "[⚡](bold green)"
#success_symbol = "[✨](bold green)"
success_symbol = "[💖](bold green)"

[cmd_duration]
min_time = 0 # Show command duration over 10,000 milliseconds (=10 sec)
format = " took [$duration]($style)"

[directory]
truncation_length = 5
format = "[$path]($style)[$lock_symbol]($lock_style)"

[git_branch]
format = " [$symbol$branch]($style) "
symbol = "🍣 "
style = "bold yellow"

[git_commit]
commit_hash_length = 8
style = "bold white"

[git_state]
format = '[\($state( $progress_current of $progress_total)\)]($style) '

[git_status]
conflicted = "⚔️ "
ahead = "🏎️ 💨 ×${count}"
behind = "🐢 ×${count}"
diverged = "🔱 🏎️ 💨 ×${ahead_count} 🐢 ×${behind_count}"
untracked = "🛤️ ×${count}"
stashed = "📦 "
modified = "📝 ×${count}"
staged = "🗃️ ×${count}"
renamed = "📛 ×${count}"
deleted = "🗑️ ×${count}"
style = "bright-white"
format = "$all_status$ahead_behind"

[hostname]
ssh_only = false
format = "<[$hostname]($style)>"
trim_at = "-"
style = "bold dimmed white"
disabled = true

[julia]
format = "[$symbol$version]($style) "
symbol = "ஃ "
style = "bold green"

[memory_usage]
format = "$symbol[${ram}( | ${swap})]($style) "
threshold = 70
style = "bold dimmed white"
disabled = false


[username]
style_user = "green"
show_always = true
format = "[$user]($style) "

[time]
disabled = false
time_format = "%R" # Hour:Minute Format
format = '[$time]($style)'
style = "bold white"
评论
0条评论

添加新评论

昵称
邮箱
网址